﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using JTicket.BLL;
using JTicket.Printer;
using System.Text;
namespace JTicket.UI
{
    public partial class S_OrderTicket : System.Web.UI.Page
    {
     private readonly   BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Bind();
        }
        void Bind()
        {
            this.txtBeginTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            string btime = this.txtBeginTime.Text;
            string etime = this.txtEndTime.Text;
            List<Model.D_TitleInfo> tileList = BLL.D_TitleInfo.
                GestListAll(bllOper.Oper().ThCode,
                DateTime.Parse(btime), DateTime.Parse(etime));
            this.DropDownList1.DataSource = tileList;
            this.DropDownList1.DataTextField = "Stitlename";
            this.DropDownList1.DataValueField = "StitleCode";
            this.DropDownList1.DataBind();

        } 
        protected void txtBeginTime_TextChanged(object sender, EventArgs e)
        {
            string btime = this.txtBeginTime.Text;
            string etime = this.txtEndTime.Text;
            List<Model.D_TitleInfo> tileList = BLL.D_TitleInfo.
                 GestListAll(bllOper.Oper().ThCode,
                 DateTime.Parse(btime), DateTime.Parse(etime));
            this.DropDownList1.DataSource = tileList;
            this.DropDownList1.DataTextField = "Stitlename";
            this.DropDownList1.DataValueField = "StitleCode";
            this.DropDownList1.DataBind();
        }
        protected void txtEndTime_TextChanged(object sender, EventArgs e)
        {
            string btime = this.txtBeginTime.Text;
            string etime = this.txtEndTime.Text;
            List<Model.D_TitleInfo> tileList = BLL.D_TitleInfo.
               GestListAll(bllOper.Oper().ThCode,
               DateTime.Parse(btime), DateTime.Parse(etime));
            this.DropDownList1.DataSource = tileList;
            this.DropDownList1.DataTextField = "Stitlename";
            this.DropDownList1.DataValueField = "StitleCode";
            this.DropDownList1.DataBind();
        }
        protected void BtnSelect_Click1(object sender, EventArgs e)
        {
            if (this.DropDownList1.Items.Count > 0)
            {
                DataSet ds = H_TicketBal.OrderInfoGet(this.TextBox1.Text, this.TextBox2.Text, this.TextBox3.Text, DropDownList1.SelectedItem.Value, bllOper.Oper().ThCode);
                this.dg1.DataSource = ds.Tables[0].DefaultView;
                this.dg1.DataBind();
            }
            else
            {
                JTicket.Common.JsMsg.Alert("请选择演出名称！");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           
            bool b = false;
            StringBuilder sb = new StringBuilder();
            string Invodio = "无";
            string ListSQL = string.Empty;
            string thcode = bllOper.Oper().ThCode;
            if (dg1.Items.Count > 0)
            {
                for (int i = 0; i <= dg1.Items.Count - 1; i++)
                {
                    CheckBox cb = (CheckBox)dg1.Items[i].Cells[0].FindControl("CheckBox1");
                    if (cb.Checked)
                    {
                        Model.SaleParm sale = new Model.SaleParm();
                        sale.ThCode = thcode;
                        sale.TitleCode = cb.ToolTip;
                        sale.UserName = dg1.Items[i].Cells[1].Text;
                        sale.UserTel = dg1.Items[i].Cells[2].Text;
                        if (BLL.D_Tax.Exists(thcode))
                        {
                            BLL.D_Tax bllTax = new BLL.D_Tax();
                            Model.D_Tax Tax = bllTax.GetModel(thcode);
                            if (!Tax.fpsy.Equals(Tax.fpsz))
                            {
                                Invodio = Tax.fpsy;

                            }
                        }
                        //生成发票号。
                        if (Invodio != "无")
                        {
                            Print.BuildTaxU(dg1.Items[i].Cells[7].Text,
                                dg1.Items[i].Cells[1].Text, int.Parse
                                (dg1.Items[i].Cells[6].Text));
                        }
                        b = H_OrderBal.OrderBookTicket(sale, bllOper.Oper().OperCode,
                            Request.UserHostAddress, "",
                            dg1.Items[i].Cells[1].Text, Invodio, "", "预订取票");

                        if (b)
                        {
                            List<Model.L_OrderBal> orderbal =
                                BLL.H_OrderBal.GetOrderInfoList
                                (dg1.Items[i].Cells[0].Text, dg1.Items[i].Cells[2].Text);
                            foreach (var m in orderbal)
                            {
                                sb.Append(Print.MakeOrderGetCmd
                                    (m.ordercode, m.concertcode, m.ThCode, m.BarCode));
                                sb.Append("|");
                            }
                            this.sqltxt.Value = sb.ToString();
                            JTicket.Common.JsMsg.Alert("操作成功！");
                            DataSet ds = H_TicketBal.OrderInfoGet(this.TextBox1.Text, this.TextBox2.Text, this.TextBox3.Text, DropDownList1.SelectedItem.Value, bllOper.Oper().ThCode);
                            this.dg1.DataSource = ds.Tables[0].DefaultView;
                            this.dg1.DataBind();
                        }
                        else
                            JTicket.Common.JsMsg.Alert("操作失败！");
                    }
                }
            }
        }
        protected void dg1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex == -1) return;
            e.Item.Attributes.Add("bgcolor", "#EFFFDA");
            e.Item.Attributes.Add("style", "cursor:hand");
            e.Item.Attributes.Add("onMouseOver=this.style.backgroundColor", "#DBE3FA");
            e.Item.Attributes.Add("onMouseOut=this.style.backgroundColor", "#F6F6F6");
            e.Item.Attributes.Add("ondblclick", "window.showModalDialog('S_OrderList.aspx?ordercode=" + e.Item.Cells[0].Text + "&usertel=" + e.Item.Cells[2].Text + "&usertext=" + e.Item.Cells[1].Text + "','','location:No;status:No;help:No;dialogWidth:900px;dialogHeight:500px;scroll:yes');");        }
        public string Gstate(string st)
        {
            switch (st)
            {
                case "1":
                    return "预订";

                default:
                    return "未知状态";
            }
        }
   
        protected void ImageButton1_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                bool b = false;
                string[] arg = e.CommandArgument.ToString().Split('|');
                Model.SaleParm sale = new Model.SaleParm();
                sale.ThCode = bllOper.Oper().ThCode;
                sale.TitleCode = arg[1];
                b = H_OrderBal.OrderBookTicket(sale, bllOper.Oper().OperCode, Request.UserHostAddress, "", arg[0], "", "", "取消预订");
                if (b)
                {
                    JTicket.Common.JsMsg.Alert("操作成功！");
                    DataSet ds = H_TicketBal.OrderInfoGet(this.TextBox1.Text, this.TextBox2.Text, this.TextBox3.Text, DropDownList1.SelectedItem.Value, "");
                    this.dg1.DataSource = ds.Tables[0].DefaultView;
                    this.dg1.DataBind();
                }
            }
        }
    }
}